import { VuexModule, getModule, Module, Mutation, Action } from 'vuex-module-decorators';
import { StartDate } from '/@/hooks/useStartDate';
import store from '/@/store';
import { IntentionData } from '/@/types';
import { ProjectConfig } from '/@/types/config';
import { loadCookie, loadStorage, saveStorage } from '/@/utils/cache';
import { LOGIN_STATUS, MY_INTENTION, PROJECT_SETTING } from '/@/utils/constants';

const defaultProjectSetting: ProjectConfig = {
  showFaith: 1,
  showFather: 1,
  showMary: 1,
  showGlory: 1,
  showQueen: 1,
  showSong: false,
  onlyShowBody: false,
  rosarySelect: ['joyful', 'luminous', 'sorrowful', 'glorious'],
};

const NAME = 'app';
@Module({ dynamic: true, namespaced: true, store, name: NAME })
export default class App extends VuexModule {
  private startDate: Partial<StartDate> = {};
  private projectSetting: ProjectConfig = loadStorage(
    PROJECT_SETTING,
    defaultProjectSetting
  ) as ProjectConfig;
  private loginStatus: number = Number(loadCookie(LOGIN_STATUS, '0'));
  private intention: Partial<IntentionData> = {};
  private myIntention: string[] = loadStorage(MY_INTENTION, []) as string[];

  get getStartDate() {
    return this.startDate;
  }
  get getProjectSetting() {
    return this.projectSetting;
  }
  get getloginStatus() {
    return this.loginStatus;
  }

  get getIntention() {
    return this.intention;
  }
  get getMyIntention() {
    return this.myIntention;
  }

  @Mutation
  commitMyIntention(myIntention: string[]) {
    const saveIntention = saveStorage(MY_INTENTION, myIntention) as string[];
    this.myIntention = saveIntention;
  }

  @Mutation
  commitIntention(intention: IntentionData) {
    this.intention = intention;
  }

  @Mutation
  commitStartDate(startDate: Partial<StartDate>) {
    this.startDate = startDate;
  }

  @Mutation
  commitProjectSetting(projectSetting: Partial<ProjectConfig>) {
    this.projectSetting = saveStorage(
      PROJECT_SETTING,
      Object.assign({}, this.projectSetting, projectSetting)
    ) as ProjectConfig;
  }
}

export const appStore = getModule<App>(App);
